package com.zeedev.flyingsaucer.beers.activities.storechooser;

import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import com.google.inject.Inject;
import com.zeedev.flyingsaucer.modules.StoreModule.impl.StoreDatabaseModel;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.DataModule.Util;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class StoreChooser extends RoboActivity {

    @InjectView(R.id.store_chooser_go_to_beers)
    private Button viewBeerLists;

    @InjectView(R.id.store_chooser_spinner)
    private Spinner storeSpinner;

    @Inject
    StoreChooserController controller;

/*    private OnClickListener viewBeerListListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(StoreChooser.this, BeerListParent.class);
            String storeUrl = controller.getModel().get(storeSpinner.getSelectedItem());
            intent.putExtra(BeerConstants.INTENT_STORE_URL, storeUrl);
            intent.putExtra(BeerConstants.INTENT_CONTROLLER_CLASS, StoresListParentController.class);
            startActivity(intent);
        }

    };*/

    public void onClickViewBeers(View view) {

        throw new IllegalArgumentException("This method is incomplete.");

/*        Intent intent = new Intent(StoreChooser.this, BeerListParent.class);
        String storeUrl = controller.getModel().get(storeSpinner.getSelectedItem());

        intent.putExtra(BeerConstants.INTENT_STORE_URL, storeUrl);
        intent.putExtra(BeerConstants.INTENT_CONTROLLER_CLASS, StoresListParentController.class);
        startActivity(intent);*/
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        setContentView(R.layout.beers_storechooser);

//        viewBeerLists.setOnClickListener(viewBeerListListener);

//        controller.getModel().markAndNotify();

//        controller = new StoreChooserController(this);

    }

    private static final ArrayAdapter<String> getStoreListAdapter2(Context mContext,
                                                                   StoreDatabaseModel storeNameUrlModel) {
        // HashMap<String,String> storeMap = s.getNameIdStoreMap();

        List<String> storeNames = new ArrayList<String>();
        for (String curStoreName : storeNameUrlModel.keySet()) {
            storeNames.add(curStoreName);
        }
        ArrayAdapter<String> aa = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_spinner_item, storeNames);
        aa
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        aa.sort(new Comparator<String>() {
            @Override
            public int compare(String object1, String object2) {
                return object1.compareTo(object2);
            }
        });

        return aa;
    }

    public void setSpinnerModel(StoreDatabaseModel model) {
        this.storeSpinner.setAdapter(getStoreListAdapter2(this, model));
    }

    public void setIndicatorVisible(Boolean visible, Boolean modal) {
        if (visible) {

            this.setProgressBarIndeterminateVisibility(true);
        } else {
            this.setProgressBarIndeterminateVisibility(false);
        }

    }

    public void setDataReady(Boolean ready) {
        this.storeSpinner.setEnabled(ready);
        this.storeSpinner.setClickable(ready);
        this.viewBeerLists.setEnabled(ready);
    }

//    public Context getContext() {
//        return this;
//    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Menu
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//		MenuInflater inflater = getMenuInflater();
//		inflater.inflate(R.menu.new_beer, menu);
//		return true;
        return Util.createMenu(this, menu, R.menu.new_beer);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.refresh_new_beer: {

                this.controller.refresh(true);

                return true;
            }
        }
        return false;
    }


/*    @Override
    public void sayToast(String message) {
        Generate.sayToast(this, message);
    }*/


}